<?php
namespace app\serveradv\logic;

use \think\Db;
use \app\manage\logic\BaseLogic;
/**
 * @Author: 蒋哲
 * @Date:   2017-05-27 16:25:06
 * @Last Modified by:   Marte
 * @Last Modified time: 2017-08-31 17:45:05
 */
class Adv extends BaseLogic
{
    protected $name = 'adv';
    protected $config = 'db_config_adv';
    protected $advModel = 'app\serveradv\model\Adv';
    protected $advClassifyModel = 'app\serveradv\model\AdvClassify';
    protected $searchWhere = ['c_id'=>'eq','name'=>'like', 'description'=>'like', 'add_user'=>'like', 'auditor'=>'like'];

    // 添加新的广告位
    public function advAdd($data)
    {
        $rst = $this->model($this->advModel)->save($data);
        $rstId = $this->model($this->advModel)->id;

        if ($rst === false) {
            return auto_failure ($data,'广告位添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未作改动！');
        }
        return auto_return(['id' => $rstId],'广告位添加成功！');
    }

    // 某广告位信息编辑修改
    public function advEdit($data)
    {
        $data['update_time'] = time();
        $rst = $this->model($this->advModel)
                    ->where('id','in',$data['id'])
                    ->update($data);
        if ($rst === false) {
            return auto_failure ($data,'广告位信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位信息未作改动！');
        }

        return auto_return(['number' => $rst],'广告位信息更改成功！');
    }

    // 删除某个广告位
    public function advInfo($data): array
    {
        $rst = $this->db()->where('id','eq',$data['id'])->find();
        if ($rst == null) {
            return auto_failure ($data,'广告位不存在！');
        }
        $rst['cate_ids'] = logic('\app\serveradv\logic\AdvClassify')->getParentIds($rst['c_id']);
        return auto_return($rst, '广告详情获取成功！');
    }

    // 删除某个广告位
    public function advDelete($data): array
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        if (intval($manageAppId) != 11) {
            $list = Db::connect(config('db_config_adv'))
                ->view('adv','*')
                ->view('adv_classify', ['name' => 'classify_name'], 'adv_classify.id = adv.c_id')
                ->where('adv.status', 'neq', -1)
                ->where('adv_classify.app_id', 'eq', intval($manageAppId))
                ->where('adv.id', 'eq', intval($data['id']))
                ->find();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作删除！');
            }
        }

        $status['status'] = -1;
        $status['update_time'] = time();
        $rst = $this->model($this->advModel)
                    ->where('id','in',$data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位删除失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生删除操作！');
        }

        return auto_return(['number' => $rst], '广告位删除成功！');
    }

    // 启用某个广告位
    public function advEnable($data): array
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        if (intval($manageAppId) != 11) {
            $list = Db::connect(config('db_config_adv'))
                ->view('adv','*')
                ->view('adv_classify', ['name' => 'classify_name'], 'adv_classify.id = adv.c_id')
                ->where('adv.status', 'neq', -1)
                ->where('adv_classify.app_id', 'eq', intval($manageAppId))
                ->where('adv.id', 'eq', intval($data['id']))
                ->find();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['status'] = 1;
        $status['update_time'] = time();
        $rst = $this->model($this->advModel)
                    ->where('id','in',$data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位启用失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位启用成功！');
    }

    // 禁用某个广告位
    public function advDisable($data): array
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        if (intval($manageAppId) != 11) {
            $list = Db::connect(config('db_config_adv'))
                ->view('adv','*')
                ->view('adv_classify', ['name' => 'classify_name'], 'adv_classify.id = adv.c_id')
                ->where('adv.status', 'neq', -1)
                ->where('adv_classify.app_id', 'eq', intval($manageAppId))
                ->where('adv.id', 'eq', intval($data['id']))
                ->find();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['status'] = 0;
        $status['update_time'] = time();
        $rst = $this->model($this->advModel)
                    ->where('id','in',$data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位禁用失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位禁用成功！');
    }

    // 审核通过某个广告位
    public function advAudit($data): array
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        if (intval($manageAppId) != 11) {
            $list = Db::connect(config('db_config_adv'))
                ->view('adv','*')
                ->view('adv_classify', ['name' => 'classify_name'], 'adv_classify.id = adv.c_id')
                ->where('adv.status', 'neq', -1)
                ->where('adv_classify.app_id', 'eq', intval($manageAppId))
                ->where('adv.id', 'eq', intval($data['id']))
                ->find();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['audit_status'] = 1;
        $status['auditor_id'] = $data['auditor_id'];
        $status['update_time'] = time();
        $rst = $this->model($this->advModel)
                    ->where('id','in',$data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位审核通过失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位审核通过成功！');
    }

    // 审核失败/不通过某个广告位
    public function advAuditFailure($data): array
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        if (intval($manageAppId) != 11) {
            $list = Db::connect(config('db_config_adv'))
                ->view('adv','*')
                ->view('adv_classify', ['name' => 'classify_name'], 'adv_classify.id = adv.c_id')
                ->where('adv.status', 'neq', -1)
                ->where('adv_classify.app_id', 'eq', intval($manageAppId))
                ->where('adv.id', 'eq', intval($data['id']))
                ->find();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['audit_status'] = -1;
        $status['auditor_id'] = $data['auditor_id'];
        $status['update_time'] = time();
        $rst = $this->model($this->advModel)
                    ->where('id','in',$data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位操作失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位操作成功！');
    }

    // 全部广告位列表获取
    public function advList($data)
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $whereOr = $manageAppId == 11 ? true : 'adv_classify.`app_id` = ' . $manageAppId . ' OR FIND_IN_SET(' . $manageAppId . ',`other_app_read`) OR adv_classify.`is_public` = 1';
        $list = Db::connect(config('db_config_adv'))
                ->view('adv','*')
                ->view('adv_classify',['name' => 'classify_name'],'adv_classify.id = adv.c_id')
                ->where('adv.status', 'neq', -1)
                ->where($whereOr)
                ->order('id', 'desc')
                ->paginate($data['listRows'], false, ['page'=>$data['page']])
                ->toArray();

        for ($i=0; $i < count($list["data"]); $i++) {
            $list["data"][$i]["create_time"] = date("Y-m-d",$list["data"][$i]["create_time"]);
            $list["data"][$i]["update_time"] = date("Y-m-d",$list["data"][$i]["update_time"]);
            if ($list["data"][$i]["status"] == -1) {
                $list["data"][$i]["status_text"] = "删除";
            } elseif ($list["data"][$i]["status"] == 0) {
                $list["data"][$i]["status_text"] = "失效";
            } else {
                $list["data"][$i]["status_text"] = "正常";
            }
            if ($list["data"][$i]["audit_status"] == -1) {
                $list["data"][$i]["audit_text"] = "审核失败";
            } elseif ($list["data"][$i]["audit_status"] == 0) {
                $list["data"][$i]["audit_text"] = "未审核";
            } else {
                $list["data"][$i]["audit_text"] = "审核通过";
            }
        }
        return auto_return_page($list);
    }

    // 获取某个分类广告位 非删除
    public function advClassList($data): array
    {
        $_where['a.status'] = ['eq',1];
        $data['c_id'] && $_where['a.c_id'] = $data['c_id'];
        $arr = $this->db()
                    ->alias('a')
                    ->field('a.*')
                    ->join('adv_classify ac', 'a.c_id = ac.id', 'LEFT')
                    ->where($_where)
                    ->order('order', 'asc')
                    ->paginate($data['listRows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($arr);
    }

    // 不同状态广告位列表获取
    public function advDifferenceStatus($data): array
    {
        $_where['status'] = ['eq',1];
        $_where['audit_status'] = $data['audit_status'];

        $arr = $this->db()
                    ->where($_where)
                    ->order('id', 'desc')
                    ->paginate($data['listRows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($arr);
    }

    // 规定上架时间内 正常状态 审核通过的广告位等
    public function advTimeLimit($data): array
    {
        if (!empty($data['c_id']) && !empty($data['action'])) {
            return auto_failure ($data,'分类id和分类标识只能传一个，不能共传！');
        }
        if (empty($data['c_id']) && empty($data['action'])) {
            return auto_failure ($data,'分类id和分类标识必须传一个，不能共传！');
        }
        if (!empty($data['action'])) {
            $rst = Db::connect(config('db_config_adv'))
                    ->view('adv_classify', 'id')
                    ->where('adv_classify.action', 'eq', $data['action'])
                    ->find();
            if ($rst == null) {
                return auto_failure($data, '分类标识不存在！');
            }
            $data['c_id'] = $rst['id'];
        }
        $_where['c_id']         = $data['c_id'];
        $_where['status']       = ['eq',1];
        $_where['audit_status'] = ['eq',1];
        $time = time();

        $arr = $this->db()
                    ->where($_where)
                    ->where('start_time','elt',$time)
                    ->where('end_time','egt',$time)
                    ->limit($data['limit'])
                    ->order('id', 'desc')
                    ->select();
        return auto_return($arr);
    }
}